<?php
/**
 * 智力挑战模块定义
 *
 * @author junsion
 * @url http://s.we7.cc/index.php?c=store&a=author&uid=74516
 */
defined('IN_IA') or exit('Access Denied');
require_once IA_ROOT.'/addons/junsion_challeng/func.php';
class Junsion_challengModule extends WeModule {

	public function settingsDisplay($settings) {
		global $_W, $_GPC;
		
		pdo_update('modules',array('wxapp_support'=>2,'app_support'=>2),array('name'=>'junsion_challeng'));
		if ($_W['account']['type'] != 4){
			echo "<script>alert('公众号版仅供看后台功能')</script>";
		}
		
		//成语题库
		$phrase = pdo_fetch('select id from ' . tablename('junsion_challeng_phrase') . " where 1 limit 1");
		if(empty($phrase)){
			$path = IA_ROOT.'/addons/junsion_challeng/phrase';
			$phrase_sql = file_get_contents($path);
			pdo_query($phrase_sql);
		}
		$pics = pdo_fetch('select id from ' . tablename('junsion_challeng_pic') . " where weid='{$_W['uniacid']}' limit 1");
		if(empty($pics)){
			$path = IA_ROOT.'/addons/junsion_challeng/pic';
			$phrase_sql = file_get_contents($path);
			$phrase_sql = str_replace('12', $_W['uniacid'], $phrase_sql);
			pdo_query($phrase_sql);
		}
		$musics = pdo_fetch('select id from ' . tablename('junsion_challeng_music') . " limit 1");
		if(empty($musics)){
			$path = IA_ROOT.'/addons/junsion_challeng/music';
			$phrase_sql = file_get_contents($path);
			pdo_query($phrase_sql);
		}
		
		//点击模块设置时将调用此方法呈现模块设置页面，$settings 为模块设置参数, 结构为数组。这个参数系统针对不同公众账号独立保存。
		//在此呈现页面中自行处理post请求并保存设置参数（通过使用$this->saveSettings()来实现）
		$wechats = pdo_fetchall('select uniacid,name from ' . tablename('account_wechats') . " where 1");
		$wechats1 = json_encode($wechats,JSON_UNESCAPED_UNICODE);
		$news = json_decode($settings['news'],true);
		foreach ($news as &$v) {
			if ($v['img']) $v['img'] = toimage($v['img']);
		}
		$settings['news'] = json_encode($news);
		$poster = unserialize($settings['poster']);
		if(checksubmit()) {
			//字段验证, 并获得正确的数据$dat
			$guide_wechat = array();
			$sel_wechat = $_GPC['wechat'];
			if(!empty($sel_wechat)){
				foreach ($sel_wechat as $k => $v){
					if(!empty($v)){
						$guide_wechat[] = array(
								'wechat' => $v,
								'link' => $_GPC['link'][$k],
						);
					}
				}
			}
			$price = array();
			$price_num = $_GPC['num'];
			if(!empty($price_num)){
				foreach ($price_num as $k => $v){
					if(!empty($v)){
						$price[] = array(
								'num' => $v,
								'price' => $_GPC['price'][$k],
								'costprice' => $_GPC['costprice'][$k],
								'des' => $_GPC['des'][$k]
						);
					}
				}
				array_multisort($_GPC['num'],SORT_ASC,$price);
			}
			$_GPC['poster']['data'] = json_decode(htmlspecialchars_decode($_GPC['poster']['data']),true);
			
			$adv = array();
			$index_picurl = $_GPC['index_picurl'];
			if (!empty($index_picurl)){
				foreach ($index_picurl as $key => $value) {
					if (!empty($value)){
						$adv[] = array(
								'pic'=>$value,
								'appid'=>$_GPC['index_appid'][$key],
								'link'=>$_GPC['index_link'][$key],
								'title'=>$_GPC['index_title'][$key],
								'desc'=>$_GPC['index_desc'][$key],
								'btn'=>$_GPC['index_btn'][$key],
								'sort'=>$_GPC['index_sort'][$key],
						);
					}
				}
				array_multisort($_GPC['index_sort'],SORT_DESC,$adv);
			}
			$home_adv = array();
			$home_picurl = $_GPC['home_picurl'];
			if (!empty($home_picurl)){
				foreach ($home_picurl as $key => $value) {
					if (!empty($value)){
						$home_adv[] = array(
								'pic'=>$value,
								'appid'=>$_GPC['home_appid'][$key],
								'link'=>$_GPC['home_link'][$key],
								'sort'=>$_GPC['home_sort'][$key],
						);
					}
				}
				array_multisort($_GPC['home_sort'],SORT_DESC,$home_adv);
			}
			
			$dat = array(
					'isnew' => $_GPC['isnew'],
					'APPKEY' => $_GPC['APPKEY'],
					'mchid' => $_GPC['mchid'],
					'base' => $_GPC['base'],
					'msg' => $_GPC['msg'],
					'msgid' => $_GPC['msgid'],
					'share0' => $_GPC['share0'],
					'share1' => $_GPC['share1'],
					'share2' => $_GPC['share2'],
					'share3' => $_GPC['share3'],
					'share4' => $_GPC['share4'],
					'share5' => $_GPC['share5'],
					'share6' => $_GPC['share6'],
					'share7' => $_GPC['share7'],
					'share8' => $_GPC['share8'],
					'share9' => $_GPC['share9'],
					'music' => $_GPC['music'],
					'err_music' => $_GPC['err_music'],
					'suc_music' => $_GPC['suc_music'],
					'get_share_num' => intval($_GPC['get_share_num']),
					'share_num' => intval($_GPC['share_num']),
					'get_click_num' => intval($_GPC['get_click_num']),
					'click_num' => intval($_GPC['click_num']),
					'guide' => $_GPC['guide'],
					'outtime' => $_GPC['outtime'],
					'outurl' => $_GPC['outurl'],
					'guide_num' => intval($_GPC['guide_num']),
					'wechat' => $guide_wechat,
					'agreement' => htmlspecialchars_decode($_GPC['agreement']),
					'agreetitle' => $_GPC['agreetitle'],
					'rewardtitle' => $_GPC['rewardtitle'],
					'font' => $_GPC['font'],
					'news'=>htmlspecialchars_decode($_GPC['news']),
					'reply' => $_GPC['reply'],
					'type' => intval($_GPC['type']),
					'imgs' => $_GPC['imgs'],
					'price' => $price,
					'poster' => serialize($_GPC['poster']),
					'redis' => $_GPC['redis'],
					'face' => $_GPC['face'],
					'game' => $_GPC['game'],
					'game1' => $_GPC['game1'],
					'game2' => $_GPC['game2'],
					'game3' => $_GPC['game3'],
					'game4' => $_GPC['game4'],
					'game5' => $_GPC['game5'],
					'game6' => isGameHost(6)?$_GPC['game6']:"",
					'game7' => isGameHost(7)?$_GPC['game7']:"",
					'game8' => isGameHost(8)?$_GPC['game8']:"",
					'game9' => isGameHost(9)?$_GPC['game9']:"",
					'appversion' => $_GPC['appversion'],
					'adv' => $adv,
					'home_adv' => $home_adv,
					'game_status' => $_GPC['game_status'],
					'clipboard' => $_GPC['clipboard'],
					'sort' => $_GPC['sort'],
					'wisepay' => $_GPC['wisepay'],
					'junsiontoken' => $_GPC['junsiontoken'],
					'wx_color' => $_GPC['wx_color'],
					'check_game' => $_GPC['check_game'],
					'advs' => htmlspecialchars_decode(str_replace('&quot;','&#039;',$_GPC ['advs']),ENT_QUOTES),
			);
			if($dat['guide_num']>0 && !empty($dat['wechat'])){
				foreach ($_GPC['wechat'] as $value) {
					$is_save = pdo_fetch('select id from ' . tablename('rule') . " where name = 'junsion_challeng{$_W['uniacid']}' and uniacid = '{$value}' ");
					if(empty($is_save)){
						$rule = array(
								'uniacid' => $value,
								'name' => 'junsion_challeng'.$_W['uniacid'],
								'module' => $this->modulename,
								'status' => 1,
								'displayorder' => 250,
						);
						pdo_insert('rule',$rule);
						$r_id = pdo_insertid();
						unset($rule['name']);
						$rule['type'] = 1;
						$rule['rid'] = $r_id;
						$rule['content'] = 'junsion_challeng'.$_W['uniacid'];
						pdo_insert('rule_keyword',$rule);
					}
				}
			}
			if ($this->saveSettings($dat)) {
				message ( '保存成功', 'refresh' );
			}
		}
		if (empty($settings['appversion'])){
			$settings['appversion'] = '1.0';
		}
		//这里来展示设置项表单
		include $this->template('setting');
	}

}
